package one.stand.service.impl;

import com.github.pagehelper.PageInfo;
import one.stand.exception.ParamException;
import one.stand.model.DynamicModel;
import one.stand.model.ResultModel;
import one.stand.model.essay.EssayRelatedModel;
import one.stand.model.essay.EssayShareRecordModel;
import one.stand.model.essay.TaskCustomerModel;
import one.stand.model.essay.TaskStaffModel;
import one.stand.request.essay.EssayRecordForm;
import one.stand.request.essay.MyTaskDetailQuery;
import one.stand.response.essay.EssayRecordResponse;
import one.stand.service.BaseService;
import one.stand.service.IDynamicService;
import one.stand.service.IEssayShareRecordService;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章分享记录 服务实现类
 * </p>
 *
 * @author luoyang
 * @since 2020-05-07
 */
@Service
public class EssayShareRecordServiceImpl extends BaseService implements IEssayShareRecordService {

    @Autowired
    private IDynamicService iDynamicService;
    /**
     * 添加或更新员工文章分享次数
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultModel<Boolean> addShareRecordData(EssayRecordForm request) throws ParamException {
        if(request.getCompanyId() == null){
            request.setCompanyId(request.getCId());
        }
        //更新动态的分享记录
        iDynamicService.updateShareNum(request.getDynamicId().intValue());

        //更新员工任务分享数据
        TaskStaffModel taskStaffModel = this.taskStaffMapper.queryMyTaskStaffDetail(new MyTaskDetailQuery(){{
            setUserId(request.getUserId());
            setDynamicId(request.getDynamicId());
        }});

        if(taskStaffModel != null){
            taskStaffModel.setShareNum(taskStaffModel.getShareNum() == null ? 1 : taskStaffModel.getShareNum()+1);
            this.taskStaffMapper.update(taskStaffModel);
            //检查如果分享次数和达到任务次数则已经完成
            TaskCustomerModel taskCustomerModel = this.taskCustomerMapper.queryById(taskStaffModel.getTaskId().intValue());
            if(taskCustomerModel != null){
                if(taskStaffModel.getVisitNum() .intValue() == taskCustomerModel.getVisitNum().intValue() && taskStaffModel.getShareNum().intValue() == taskCustomerModel.getShareNum().intValue()){
                    taskStaffModel.setState(1);
                    this.taskStaffMapper.update(taskStaffModel);
                }
            }else{
                log.info("TaskCustomerModel_error:{}",taskStaffModel.getTaskId());
            }
        }

        EssayShareRecordModel model = this.essayShareRecordMapper.queryRecord(request);
        if(model == null){
            //分享一次算一次数据。之前的分享数据累加
            model = new EssayShareRecordModel();
            BeanUtils.copyProperties(request,model);
            model.setCreatedTime(DateUtil.now());
            model = countModel(model,request);
            if(this.essayShareRecordMapper.insert(model) > 0){
                addGoods(request.getGoods(),model);
                addDynamics(request.getDynamics(),model);
                return ResultModel.success();
            }
            return ResultModel.fail();
        }else if(request.getType() != null){
            model = countModel(model,request);
            model.setCreatedTime(DateUtil.now());
            if(this.essayShareRecordMapper.update(model) > 0){
                //先删除所有 在创建
                addGoods(request.getGoods(),model);
                addDynamics(request.getDynamics(),model);
                return ResultModel.success();
            }
        }
        return ResultModel.fail();
    }



    /**
     * 添加分享商品明细
     * @param goods
     * @return
     */
    private void addGoods(final List<Integer> goods,final EssayShareRecordModel model) throws ParamException {
        if(goods == null){
            return;
        }
        goods.forEach(good ->{
            if(good != null){
                EssayRelatedModel essayRelatedModel = new EssayRelatedModel();
                essayRelatedModel.setCreatedTime(DateUtil.now());
                essayRelatedModel.setRelatedType(1L);
                essayRelatedModel.setGoodsId(Long.valueOf(good));
                essayRelatedModel.setRecordId(model.getId());
                if(this.essayRelatedMapper.addGood(essayRelatedModel) == 0){
                   //todo throw exception
                }
            }
        });
    }

    /**
     * 添加分享商品文章明细
     * @param dynamics
     * @return
     */
    private void addDynamics(final List<Integer> dynamics,final EssayShareRecordModel model) throws ParamException {
        if(dynamics == null){
            return;
        }
        dynamics.forEach(dynamic ->{
            if(dynamic != null){
                EssayRelatedModel essayRelatedModel = new EssayRelatedModel();
                essayRelatedModel.setCreatedTime(DateUtil.now());
                essayRelatedModel.setRelatedType(2L);
                essayRelatedModel.setDynamicId(Long.valueOf(dynamic));
                essayRelatedModel.setRecordId(model.getId());
                if(this.essayRelatedMapper.addDynamic(essayRelatedModel) == 0){
                    //todo throw exception
                }
            }
        });
    }



    /**
     * 计算分享 获客 浏览次数
     * 可更换为map处理
     * @param model
     * @param request
     * @return
     */
    private EssayShareRecordModel countModel( EssayShareRecordModel model,EssayRecordForm request){
//        操作类型:1分享次数 2获客数 3浏览次数
        switch (request.getType()){
            case 1:
                model.setShareNum(model.getShareNum() + 1);
                break;
            case 2:
                model.setWinCustomers(model.getWinCustomers() + 1);
                break;
            case 3:
                model.setSeeNum(model.getSeeNum() + 1);
                break;
        }
        updateDynamicModel(request);
        return model;
    }

    private void updateDynamicModel(EssayRecordForm request){
        //更新文章分享次数
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(request.getDynamicId().intValue());
        if(dynamicModel == null){
            //throw new Exception
//            return ResultModel.fail("文章错误");
        }else {
//            操作类型:1分享次数 2获客数 3浏览次数
            switch (request.getType()){
                case 1:
                    dynamicModel.setShareNum(dynamicModel.getShareNum() == null ? 0 + 1 : dynamicModel.getShareNum() + 1);
                    break;
            }

            this.dynamicMapper.updateByPrimaryKeySelective(dynamicModel);
        }
    }
}
